package com.gwtplatform.samples.hplace.server;

import com.google.inject.Inject;

import com.guit.client.command.action.CommandException;
import com.guit.client.command.action.Handler;
import com.gwtplatform.samples.hplace.shared.GetProduct;
import com.gwtplatform.samples.hplace.shared.GetProductResult;
import com.gwtplatform.samples.hplace.shared.Product;

public class GetProductHandler implements Handler<GetProduct, GetProductResult> {

    private final ProductDatabase database;

    @Inject
    public GetProductHandler(ProductDatabase database) {
        this.database = database;
    }

    @Override
    public GetProductResult handle(final GetProduct action) throws CommandException {
        Product product = database.get(action.getId());
        if (product == null) {
            throw new CommandException("Product not found");
        }
        return new GetProductResult(product);
    }
}
